package goldeye

import (
	"fmt"

	"goweb/ext/settings"
	"goweb/utils/commons"

	"github.com/globalsign/mgo/bson"
)

type FeedBack struct {
	Id        string  `bson:"_id"`
	SecuCode  string  `bson:"secu_code"`
	Public    string  `bson:"publish_time"`
	Source    string  `bson:"source"`
	ShowUrl   string  `bson:"show_url"`
	OriHref   string  `bson:"annex_url"`
	ReplyHref []Reply `bson:"annex_reply"`

	Title string `bson:"title"`
}

type Reply struct {
	ReplyUrl string `bson:"reply_url"`
	RepTitle string `bson:"title"`
	OrepUrl  string `bson:"Oreply_url"`
}

var db = "jghj_copy"
var doc = "rules"

func GetFeedBack(page, size int, m map[string]interface{}) (bool, int, interface{}) {
	c := conf.MgoConn.DB(doc).C(db)
	//ms := bson.M{}
	query := bson.M{}
	if _, ok := m["startTime"]; ok {
		query["publish_time"] = bson.M{"$gte": m["startTime"], "$lt": m["endTime"]}
		delete(m, "startTime")
		delete(m, "endTime")
	}

	for k, v := range m {
		query[k] = v
	}

	var fb []FeedBack
	count, err := c.Find(query).Count()
	if !commons.CheckErr(err) {
		return false, 0, err
	}
	all := c.Find(query).Skip(page * size).Limit(size).Sort("-publish_time").All(&fb)
	if !commons.CheckErr(all) {
		return false, 0, err
	}
	return true, count, fb
}

func UpdateFeedBack(ids string, m map[string]interface{}) bool {
	c := conf.MgoConn.DB(doc).C(db)
	query := bson.M{}
	for k, v := range m {
		query[k] = v
		fmt.Println(k, v)
	}
	update := c.Update(bson.M{"_id": ids}, bson.M{"$set": query})
	if commons.CheckErr(update) {
		return true
	}
	return false
}
